package business

import (
	"fmt"
	"ginBasic/app/common/model"
	"ginBasic/app/common/model/filter"
	"ginBasic/global"
	"regexp"
	"strings"
)

var Index = new(IndexBus)

type IndexBus struct {
	BaseBus
}

func (b IndexBus) GetLeftMenu() any {
	where := "1=1 AND type=1 AND status=1"
	if global.USERINFO.IsSuper != 1 {
		roleAccessMap := b.GetAccessAndRole(global.USERINFO.RoleId)
		s := fmt.Sprintf("%v", roleAccessMap) //数组切片直接转成字符串
		s_slice := regexp.MustCompile(`[\w.]+`).FindAllString(s, -1)
		roleAccessIds := strings.Join(s_slice, ",")
		where += " AND id IN (" + roleAccessIds + ")"
	}
	accessList := model.AccessModel.List(where)
	List := make([]*filter.AccessNode, 0)
	for _, v := range accessList {
		List = append(List, &filter.AccessNode{
			Id:       v.Id,
			ParentId: v.ModuleId,
			Url:      v.Url,
			VIcon:    v.Icon,
			Name:     v.ModuleName,
			Children: nil,
		})
	}
	menuList := Access.GetTreeRecursive(List, 0)
	return menuList
}
